package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/jackpot"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
)

// 获取jackpot大奖信息 8670
func (p *Player) JackpotInfoRequest(req *sp.JackpotInfoRequest) (res *sp.JackpotInfoResponse, err error) {
	ok := jackpot.Module.HasActive()
	if !ok {
		res = &sp.JackpotInfoResponse{}
		return
	}
	m := lib.GetJackpotInfo(p.User)
	mT := make(map[int64]int64)
	for key, val := range m {
		jptype, _ := lib.GetJackpotType(key)
		mT[jptype] = val
	}
	res = &sp.JackpotInfoResponse{
		Bonus: lib.FormatBaseKVLst(mT),
	}
	return
}

// 抽奖jackpot大奖信息 8671
func (p *Player) WinJackpotRequest(req *sp.WinJackpotRequest) (res *sp.WinJackpotResponse, err error) {
	ok := jackpot.Module.HasActive()
	if !ok {
		res = &sp.WinJackpotResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}
	spinT := int64(1)
	if spin, ok := p.GetData("slotSpin"); ok {
		spinT = spin.(int64)
		if spinT > 3 {
			spinT = 3
		}
	}

	jTypT := int64(0)
	if jTyp, ok := p.GetData("gmJackpotTyp"); ok {
		jTypT = jTyp.(int64)
	}

	kv, plate, err := lib.LuckDraw(p.User, spinT, jTypT)

	jptype, err := lib.GetJackpotType(kv.Key)
	if err != nil {
		res = &sp.WinJackpotResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}

	uProp, resGoods, boxResList, err := lib.GiveGoodsByKv("WinJackpotRequest", p.User, m.BaseKV{Key: kv.Key, Val: kv.Val}, p.Send)
	if err != nil {
		res = &sp.WinJackpotResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}
	jpvalue := int64(0)
	if jptype > 0 {
		jpvalue = kv.Val

		utils.AsyncFunc(func() {
			jackpot.Module.JackpotGamerList(p.User, jpvalue, jptype, utils.TNow().Unix())
		})
		utils.AsyncFunc(func() {
			p.WinJackpotNotice(m.BaseKV{Key: jptype, Val: jpvalue}) // 获取彩金，通知全服玩家
		})
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	res = &sp.WinJackpotResponse{
		Res:     lib.FormatGoodsInfoSps(resGoods),
		BoxRes:  lib.FormatBoxResSps(boxResList),
		Plate:   plate,
		Jptype:  jptype,
		Jpvalue: jpvalue,
		Spin:    spinT,
	}
	return
}

// jackpot中奖玩家名单 8672
func (p *Player) WinJackpotPlayerListRequest(req *sp.WinJackpotPlayerListRequest) (res *sp.WinJackpotPlayerListResponse, err error) {
	ok := jackpot.Module.HasActive()
	if !ok {
		res = &sp.WinJackpotPlayerListResponse{
			Errcode: data.Errcode.ServerErr,
		}
		return
	}

	lst, rich := jackpot.Module.GetJackpotRank()

	res = &sp.WinJackpotPlayerListResponse{
		Lst:  lib.FormatJackpotRank(lst),
		Rich: lib.FormatJackpotGamer(rich),
	}
	return
}

// jackpot中奖，通知该组人员大奖池金额 8673
func (p *Player) JackpotChangeNotice() {
	m := lib.GetJackpotInfo(p.User)
	req := &sp.JackpotChangeNoticeRequest{
		Bonus: lib.FormatBaseKVLst(m),
	}
	p.Send(req)
	// uid := p.User.Uid.Hex()
	// uids := lib.GetJackpotGroup(p.User)
	// for _, item := range uids {
	// 	if item == uid { //过滤掉自己
	// 		continue
	// 	}
	// 	if tp, ok := PlayersModule.GetPlayer(item); ok {
	// 		tp.Send(req)
	// 	}
	// }
}

// jackpot中奖，通知全服玩家 8674
func (p *Player) WinJackpotNotice(kv m.BaseKV) {
	u := p.User
	req := &sp.WinJackpotNoticeRequest{
		Name:        u.Name,
		Head:        u.Head,
		AvatarFrame: u.AvatarFrame,
		Bonus: &sp.BaseKV{
			Key: kv.Key,
			Val: kv.Val,
		},
	}

	lst := PlayersModule.GetOnlinePlayers()
	for _, item := range lst {
		if u.Uid == item.User.Uid {
			continue
		}
		item.Send(req)
	}
}
